<template>
    <!-- <a-modal
      title="Title"
      :visible="true"
    >
      <p>xxx</p>
    </a-modal> -->
  <div v-loading="loading">
    <a-modal 
      title="竣工设置" 
      :visible="visible" 
      :width="800"
      :confirmLoading="loading"
      @ok="handleOk"
      @cancel="$emit('cancel')">
      <a-form-model
        ref="ruleForm"
        :model="formData"
        :rules="rules"
        v-bind="formLayout"
      >
        <a-row>
          <a-col :span="24" >
            <a-form-model-item
              label="实际竣工时间"
              prop="completionTime"
            >
              <a-date-picker
                v-model="formData.completionTime"
                value-format="YYYY-MM-DD"
                :disabled="editable"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="备注"
              prop="remark"
            >
              <a-textarea
                :disabled="editable"
                v-model="formData.remark"
                placeholder="请输入"
                :maxLength="300"
                :auto-size="{ minRows: 3, maxRows: 6 }"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="附件"
              prop="fileIds"
            >
              <l-table
                :disabled="editable"
                :upload-arrys="formData.fileIds"
                business-code="MM16"
                count="12"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
      <template slot="footer">
        <a-button @click="$emit('cancel')">关 闭</a-button>
        <a-button v-if="!editable" @click="handleOk" type="primary">提 交</a-button>
      </template>
    </a-modal>
    <selectApprover
      :business-id="businessId"
      :process-config="setProcessConfig"
      @closeDialog="closeDialog"
    />
  </div>
</template>
<script>
import pick from 'lodash.pick'
import { settingProjectCompletion, projectCompletionDetail } from '@/api/projects'
export default {
	name:'a'+Date.now(),
    props: {
        visible: {
          type: Boolean,
          default: false
        },
        row: {
          type: Object
        }
    },
    data() {
        return {
            loading: false,
            isSubmit: false,
            formLayout: {
              labelCol: {
                xs: { span: 24 },
                sm: { span: 7 }
              },
              wrapperCol: {
                xs: { span: 24 },
                sm: { span: 13 }
              }
            },
            formData: {
              completionTime: '',
              remark: '',
              fileIds: []
            },
            rules: {
              completionTime: { required: true, message: '请选择实际竣工时间', trigger: 'change' },
              // fileIds: {type: 'array', required: true, message: '附件不能为空', trigger: 'change'},
            },
            setProcessConfig: null,
            processConfig: null,
            businessId: '',
            editable: false,
        }
    },
    watch: {
      async 'visible'(val) {
          if(val) {
            this.getEditInfo()
            this.processConfig = await this.$getConfigFn('project_completion')
            console.log('this.processConfig', this.processConfig)
            this.setProcessConfig = null
          }else {
            this.setProcessConfig = null
            this.formData = {
              completionTime: '',
              remark: '',
              fileIds: []
            }
          }
        }
    },
    methods: {
      closeDialog() {
        this.$emit("cancel")
      },
      // 获取编辑回显数据
        getEditInfo() {
          if(this.row.projectCompletionId) {
            projectCompletionDetail(this.row.projectCompletionId).then(res=> {
              if(res.code==200) {
                this.formData = pick(res.data, Object.keys(this.formData))
                this.formData.fileIds = res.data.fileList;
                this.editable = res.data.auditStatus==='SUBMITTED' || res.data.auditStatus==='PAST'
              }
            })
          }else {
            if(this.row.statusCode == 8 || this.row.statusCode == 9) {
              this.editable = true
            }else {
              this.editable = false
            }
          }
        },
        handleOk() {
          this.$refs.ruleForm.validate((valid, object) => {
            if (valid) {
                let params = JSON.parse(JSON.stringify(this.formData))
                params.projectId = this.row.projectId
                this.loading = true
                if(params.fileIds.length === 0) return this.$noticeWarning('请上传附件')
                let arr = [];
                params.fileIds.map((item)=> {
                  arr.push(item.commonId);
                })
                params.fileIds = arr;
                this.submitApprovalConfirm(this.processConfig, (flag)=>{
                    if(!flag) {
                      this.loading = false
                      return
                    }
                    settingProjectCompletion(params).then(res => {
                      if (res.code === 200) {
                          this.$message.success('设置成功')
                          this.businessId = res.data.id;
                          this.setProcessConfig = this.processConfig
                      }
                      this.loading = false
                  })
                })
            } else {
                this.$noticeWarning('请填写完整')
                return false
            }
          })
        },
    }
}
</script>